package com.situ.db;

import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AppListActivity extends Activity {
	private ListView mListView;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_app_list);
		mListView = (ListView) findViewById(R.id.listView);

		// 获取系统里面所有桌面的应用
		List<ResolveInfo> list = getAppInfos();
		AppListAdapter listAdapter = new AppListAdapter(list);
		mListView.setAdapter(listAdapter);
	}

	/**
	 * 获取所有应用的信息
	 * ResolveInfo封装了应用详细信息，是能够拿到应用的图标和应用的名字
	 * @return
	 */
	private List<ResolveInfo> getAppInfos() {
		// Intent:在Android里面有点类似于信使的感觉
		PackageManager manager = this.getPackageManager();
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		List<ResolveInfo> list = manager.queryIntentActivities(intent, 0);
		for (ResolveInfo info : list) {
			System.out.println(info);
		}
		return list;
	}


	// 不使用系统默认提供的ArrayAdapter和SimpleAdapter，而是继承BaseAdapter，自定义Adapter
	// 自己去完成数据Data和ItemLayout布局的绑定过程
	public class AppListAdapter extends BaseAdapter{
		private List<ResolveInfo> appInfos;

		public AppListAdapter(List<ResolveInfo> appInfos) {
			this.appInfos = appInfos;
		}

		// 有多少条数据
		@Override
		public int getCount() {
			return appInfos.size();
		}

		// 返回当前position位置的这一条
		@Override
		public Object getItem(int position) {
			return appInfos.get(position);
		}

		// 返回当前position位置的这一条的id
		@Override
		public long getItemId(int position) {
			return position;
		}

		// 处理View-Data数据填充的过程
		// 渲染的过程是一行一行渲染
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder = new ViewHolder();
			if (convertView == null) {
				LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = layoutInflater.inflate(R.layout.app_list_item, null);
				viewHolder.appIconImageView = (ImageView) convertView.findViewById(R.id.iv_app_icon);
				viewHolder.appNameTextView = (TextView) convertView.findViewById(R.id.tv_app_name);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			// 把数据Data和View绑定
			// ResolveInfo封住了应用的所有信息
			final ResolveInfo resolveInfo = appInfos.get(position);
			// 得到应用的名字
			final String appName = (String) resolveInfo.activityInfo.loadLabel(getPackageManager());
			// 得到应用的图标
			Drawable appIcon = resolveInfo.activityInfo.loadIcon(getPackageManager());
			viewHolder.appNameTextView.setText(appName);
			viewHolder.appIconImageView.setImageDrawable(appIcon);

			// 添加点击事件
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// Toast.makeText(AppListActivity.this, "您点击了应用：" + appName, Toast.LENGTH_SHORT).show();
					// 打开指定的应用
					String packageName = resolveInfo.activityInfo.packageName;
					String className = resolveInfo.activityInfo.name;
					ComponentName componentName = new ComponentName(packageName, className);
					Intent intent = new Intent();
					intent.setComponent(componentName);
					startActivity(intent);
				}
			});

			return convertView;
		}


		// 封装Item Layout的控件
		public class ViewHolder{
			public ImageView appIconImageView;
			public TextView appNameTextView;
		}
	}

}
